package com.ikkong.platform.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ikkong.core.dao.Blade;
import com.ikkong.core.dao.Db;
import com.ikkong.core.jfinal.ext.kit.JsonKit;
import com.ikkong.core.jfinal.ext.shiro.ClearShiro;
import com.ikkong.core.toolbox.Record;
import com.ikkong.system.controller.base.UrlPermissController;
import com.ikkong.platform.meta.intercept.TemplateIntercept;
import com.ikkong.platform.model.BankBussiness;
import com.ikkong.platform.model.BaseAttr;
import com.ikkong.platform.model.Template;
import com.ikkong.platform.model.TemplateAttr;
import com.ikkong.platform.service.BankBussinessService;
import com.ikkong.platform.service.BaseAttrService;
import com.ikkong.platform.service.TemplateService;
import com.ikkong.platform.service.impl.BankBussinessServiceImpl;
import com.ikkong.platform.service.impl.BaseAttrServiceImpl;
import com.ikkong.platform.service.impl.TemplateServiceImpl;

/**
 * Generated by Blade. 2018-06-13 11:30:29
 */
public class TemplateController extends UrlPermissController {
	private static String CODE = "template";
	private static String PERFIX = "tb_template";
	private static String LIST_SOURCE = "Template.list";
	private static String BASE_PATH = "/platform/template/";

	TemplateService service = new TemplateServiceImpl();
	BankBussinessService bankBussinessService = new BankBussinessServiceImpl();
	BaseAttrService baseAttrService = new BaseAttrServiceImpl();

	public void index() {
		setAttr("code", CODE);
		render(BASE_PATH + "template.html");
	}

	public void add() {
		setAttr("code", CODE);
		render(BASE_PATH + "template_add.html");
	}

	public void edit() {
		String id = getPara(0);
		Template template = service.findById(id);
		setAttr("model", JsonKit.toJson(template));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "template_edit.html");
	}

	public void view() {
		String id = getPara(0);
		Template template = service.findById(id);
		setAttr("model", JsonKit.toJson(template));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "template_view.html");
	}

	public void list() {
		Object grid = paginate(LIST_SOURCE, new TemplateIntercept());
		renderJson(grid);
	}

	public void save() {
		Template template = mapping(PERFIX, Template.class);
		
		int template_id = service.saveRtId(template);
		if (template_id>0) {
			int category_id = template.getCategory_id();
			if (category_id != 23) {
				// 添加模板属性，从百度自带属性中取
				List<BaseAttr> baseAttrs = baseAttrService.findBy(
						"category_id=#{category_id}",
						Record.create().set("category_id", category_id));
				//List<TemplateAttr> templateAttrList=new ArrayList<TemplateAttr>();
				Blade templateAttrBlade = Blade.create(TemplateAttr.class);
				Date now=new Date();
				for(BaseAttr baseAttr : baseAttrs){
					TemplateAttr templateAttr=new TemplateAttr();
					templateAttr.setBank_id(template.getBank_id());
					templateAttr.setBussiness_id(template.getBussiness_id());
					templateAttr.setCname(baseAttr.getCname());
					templateAttr.setCreateTime(now);
					templateAttr.setEname(baseAttr.getEname());
					templateAttr.setIsMust(1);
					templateAttr.setModifyTime(now);
					templateAttr.setOrder(0);
					templateAttr.setStatus(1);
					templateAttr.setTemplate_id(template_id);
					templateAttrBlade.save(templateAttr);
				}
			}
			// 查看是否有相关
			renderJson(success(SAVE_SUCCESS_MSG));
		} else {
			renderJson(error(SAVE_FAIL_MSG));
		}
	}

	public void update() {
		Template template = mapping(PERFIX, Template.class);
		// template.setVersion(template.getVersion()+1);
		boolean temp = service.update(template);
		if (temp) {
			renderJson(success(UPDATE_SUCCESS_MSG));
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}

	public void remove() {
		String ids = getPara("ids");
		int cnt = service.deleteByIds(ids);
		if (cnt > 0) {
			renderJson(success(DEL_SUCCESS_MSG));
		} else {
			renderJson(error(DEL_FAIL_MSG));
		}
	}

	/**
	 * 获取银行业务
	 * */
	@ClearShiro
	public void getBussinessList() {
		String id = getPara(0);

		List<BankBussiness> bankBussinessList = null;
		if (id.isEmpty()) {
			renderJson(error(SAVE_FAIL_MSG));
		} else {
			bankBussinessList = bankBussinessService.findBy("id,name",
					"bank_id=#{bank_id}", Record.create().set("bank_id", id));
			renderJson(bankBussinessList);
		}
	}

	/**
	 * 获取银行某业务下的所有模板
	 * */
	@ClearShiro
	public void getTemplateList() {
		String id = getPara(0);

		List<Template> templateList = null;
		if (id.isEmpty()) {
			renderJson(error(SAVE_FAIL_MSG));
		} else {
			templateList = service.findBy("id,name",
					"bussiness_id=#{bussiness_id}",
					Record.create().set("bussiness_id", id));
			renderJson(templateList);
		}
	}
}
